﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

namespace ListBox
{
    struct User {
        internal int id;
        internal string name;
    }

    public partial class ListBox : Form
    {
        public ListBox()
        {
            InitializeComponent();
        }

        // 手动添加 item 
        private void btn1_Click(object sender, EventArgs e)
        {
            ckl1.Items.Add("A");
            ckl1.Items.Add("B", true);
            ckl1.Items.Add("C", false);
            ckl1.Items.Add("D", CheckState.Indeterminate);
        }

        // 清除 item
        private void button1_Click(object sender, EventArgs e)
        {
            ckl1.Items.Clear();
        }

        private DataSet createDs()
        {
            DataTable t = new DataTable("emp");

            DataColumn idDataColumn = new DataColumn("id", typeof(int));
            DataColumn nameDataColumn = new DataColumn("name", typeof(string));

            t.Columns.Add(idDataColumn);
            t.Columns.Add(nameDataColumn);
            t.PrimaryKey = new DataColumn[]{idDataColumn};

            DataRow row;
            row = t.NewRow();
            row["id"] = 100;
            row["name"] = "colin";

            t.Rows.Add(row);

            row = t.NewRow();
            row["id"] = 200;
            row["name"] = "nancy";

            t.Rows.Add(row);


            DataSet ds = new DataSet("store");
            ds.Tables.Add(t);


            return ds;
        }

        // 设置 DataSet 作为数据源
        private void button2_Click(object sender, EventArgs e)
        {
            DataSet ds = createDs();
            ckl1.DataSource = ds.Tables["emp"];
            ckl1.DisplayMember = "name";
            ckl1.ValueMember = "id";
        }

        private void button6_Click(object sender, EventArgs e)
        {
            ckl1.Items.Insert(1, "插入");
        }

        // 反选第二个 item
        private void button4_Click(object sender, EventArgs e)
        {
            if(ckl1.GetItemChecked(1) == true)
                ckl1.SetItemChecked(1, false);
            else
                ckl1.SetItemChecked(1, true);
        }

        private void button3_Click_1(object sender, EventArgs e)
        {

            for (int i = 0; i < ckl1.Items.Count; i++)
            {
                ckl1.SetItemChecked(i, true);
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            ckl1.Items.RemoveAt(1);
        }

        private void button7_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < ckl1.Items.Count; i++)
            {
                if (ckl1.GetItemChecked(i) == true)
                {
                    Debug.WriteLine(ckl1.GetItemText(ckl1.Items[i]));
                }
            }
        }

        private void button8_Click(object sender, EventArgs e)
        {

            for (int i = 0; i < ckl1.Items.Count; i++)
            {
                if (ckl1.GetItemChecked(i) == true)
                {
                    Debug.WriteLine(((DataRowView)ckl1.Items[i])["id"]);
                }
            }
        }
    }
}
